﻿Imports Library

Namespace HomeApp

    <Authorize()>
    Public Class TransactionController
        Inherits System.Web.Mvc.Controller

        Function Create(ByVal owner As String) As ActionResult
            If String.IsNullOrEmpty(owner) Then
                owner = "IA"
            End If
            ViewData.Add("Owner", owner)
            Return View()
        End Function

        <HttpPost()> _
        Function Create(ByVal item As Transaction) As ActionResult
            item.ModifyBy = "System"
            item.ModifyDatetime = Now()
            Try
                item.Create()
                Return RedirectToAction("Index", "AccountPeriod", New With {.owner = item.Owner, .period = item.RecordDateToString})
            Catch
                Return View()
            End Try
        End Function

        Function Edit(ByVal id As Integer) As ActionResult
            Dim item As Transaction = New Transaction(id)
            If item.Id = 0 Then
                Return HttpNotFound()
            Else
                Return View(item)
            End If
        End Function

        <HttpPost()> _
        Function Edit(ByVal id As Integer, ByVal item As Transaction) As ActionResult
            If item.Id = 0 Then
                Return HttpNotFound()
            Else
                item.Update()
                Return RedirectToAction("Index", "AccountPeriod", New With {.owner = item.Owner, .period = item.RecordDateToString})
            End If
        End Function

        Function Delete(ByVal id As Integer) As ActionResult
            Dim item As Transaction = New Transaction(id)
            If item.Id = 0 Then
                Return HttpNotFound()
            Else
                item.Delete()
                Return RedirectToAction("Index", "AccountPeriod", New With {.owner = item.Owner, .period = item.RecordDateToString})
            End If
        End Function

    End Class
End Namespace